﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using wrsm.wow.entity;

namespace wrsm.wow {

    /// <summary>
    /// Manages World of Warcraft Installations
    /// </summary>
    [CLSCompliant(true)]
    public class WowInstallationManager {

        /// <summary>
        /// Wow Installation utility
        /// </summary>
        private WowInstallationUtility _wowInstallationUtility;

        /// <summary>
        /// A collection of managed installations
        /// </summary>
        private Dictionary<string, WowInstallation> _managedInstallations;

        public WowInstallationManager() {
            _wowInstallationUtility = new WowInstallationUtility();
            _managedInstallations = new Dictionary<string, WowInstallation>();
            //TODO: Add some functionality that will allow this information to be loaded up with the application
        }

        /// <summary>
        /// Returns reference to all managed wow installations
        /// </summary>
        /// <returns>array of wow installations</returns>
        public WowInstallation[] GetAllManagedInstallations() {
            return _managedInstallations.Values.ToArray();
        }

        /// <summary>
        /// Add a World of Warcraft Installation to manager
        /// </summary>
        /// <param name="install">Installation to add to the manager</param>
        public void AddInstall(WowInstallation install) {
            //Check that the installation location is valid
            /**
             * This might be redundant when adding an install for the first time, but we plan
             * on saving installs, and I'd like to know if somethign changed.  Wow being uninstalled, moved or whatever
             */
            //Update the wow isntallation valid flag, while testing if the flag has changed.
            bool validInstallChanged = (install.ValidInstall == ValidateWowInstallation(install));
            //Check if this is a new installation
            bool newInstallation = !_managedInstallations.ContainsKey(install.InstallLocation);
            //Update our current refernce to the install
            _managedInstallations[install.InstallLocation] = install;
            if (validInstallChanged || newInstallation) {
                //TODO: nofify of the change
            }
            throw new NotImplementedException("Must create an event to notify of the added wow installation, or of a changing status");
        }

        /// <summary>
        /// Remove a Wow Installation from the manager
        /// </summary>
        /// <param name="install">installation to remove from the manager</param>
        public void RemoveInstall(WowInstallation install) {
            if (_managedInstallations.ContainsKey(install.InstallLocation))
                _managedInstallations.Remove(install.InstallLocation);
            throw new NotImplementedException("Nortify that a wow installation is no longer managed");
        }

        /// <summary>
        /// Validates a world of warcraft Installation
        /// </summary>
        /// <param name="install">world of warcraft installation to validate</param>
        private bool ValidateWowInstallation(WowInstallation install) {
            return install.ValidInstall = _wowInstallationUtility.IsValidWowInstall(install);
        }

    }
}
